<?php
/**
 * 安装程序
 * $Id: install.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
 */

define('IN_APP', true);

require dirname(__FILE__) . '/includes/init.php';

/* 初始化语言变量*/
$installer_lang = isset($_REQUEST['lang']) ? trim($_REQUEST['lang']) : 'zh_cn';
if ($installer_lang != 'zh_cn' && $installer_lang != 'zh_tw' && $installer_lang != 'en_us') {
    $installer_lang = 'zh_cn';
}
/* 加载语言包*/
$installer_lang_package_path = ROOT_PATH . '/install/languages/' . $installer_lang . '.php';
if (file_exists($installer_lang_package_path)) {
	require_once $installer_lang_package_path;
	$smarty->assign('lang', $_LANG);
} else {
	die('Can\'t find langage package');
}

/* 定义URL*/
$root_url = root_url();
$smarty->assign('root_url', $root_url);

/* 初始化流程控制*/
$step = isset($_REQUEST['step']) ? $_REQUEST['step'] : 'welcome';
if (file_exists(ROOT_PATH . '/data/install.lock')) {
	$step = 'error';
	$err->add($_LANG['has_locked_installer']);
}

switch ($step) {
	case 'welcome' :
		$smarty->assign('installer_lang', $installer_lang);
		$smarty->display('welcome.tpl');
		break;
	case 'check' :
		require_once ROOT_PATH . '/install/includes/lib_env_checker.php';
		require_once ROOT_PATH . '/install/includes/checking_dirs.php';
		$dir_checking = check_dirs_priv($checking_dirs);
		/* 下一步按钮是否为灰色*/
		$disabled = '';
		if ($dir_checking['result'] = 'ERROR' || function_exists('mysql_connect')) {
			$disabled = 'disabled="true"';
		}

		$smarty->assign('installer_lang', $installer_lang);
		$smarty->assign('system_info', get_system_info());
		$smarty->assign('dir_checking', $dir_checking['detail']);
		$smarty->assign('disabled', $disabled);
		$smarty->display('checking.tpl');
		break;
	case 'setting_ui' :
		$prefix = 'wp_';
		$smarty->assign('timezones', get_timezone_list($installer_lang));
		$smarty->display('setting.tpl');
		break;
	case 'get_db_list' :
		$db_host = isset($_POST['db_host']) ? trim($_POST['db_host']) : '';
		$db_port = isset($_POST['db_port']) ? trim($_POST['db_port']) : '';
		$db_user = isset($_POST['db_user']) ? trim($_POST['db_user']) : '';
		$db_pass = isset($_POST['db_pass']) ? trim($_POST['db_pass']) : '';

		$databases = get_db_list($db_host, $db_port, $db_user, $db_pass);
		if ($databases === false) {
			echo json_encode(implode(',', $err->get_all()));
		} else {
			$result = array('msg'=>'OK', 'list'=>implode(',',$databases));
			echo json_encode($result);
		}
		break;
	case 'create_config_file' :
		$db_host    = isset($_POST['db_host'])      ?   trim($_POST['db_host']) : '';
		$db_port    = isset($_POST['db_port'])      ?   trim($_POST['db_port']) : '';
		$db_user    = isset($_POST['db_user'])      ?   trim($_POST['db_user']) : '';
		$db_pass    = isset($_POST['db_pass'])      ?   trim($_POST['db_pass']) : '';
		$db_name    = isset($_POST['db_name'])      ?   trim($_POST['db_name']) : '';
		$prefix     = isset($_POST['db_prefix'])    ?   trim($_POST['db_prefix']) : '';
		$timezone   = isset($_POST['timezone'])     ?   trim($_POST['timezone']) : 'Asia/Shanghai';

		$result = create_config_file($db_host, $db_port, $db_user, $db_pass, $db_name, $prefix, $timezone);
		if ($result === false) {
			echo implode(',', $err->get_all());
		} else {
			echo 'OK';
		}
		break;
	case 'create_database' :
		$db_host    = isset($_POST['db_host'])      ?   trim($_POST['db_host']) : '';
		$db_port    = isset($_POST['db_port'])      ?   trim($_POST['db_port']) : '';
		$db_user    = isset($_POST['db_user'])      ?   trim($_POST['db_user']) : '';
		$db_pass    = isset($_POST['db_pass'])      ?   trim($_POST['db_pass']) : '';
		$db_name    = isset($_POST['db_name'])      ?   trim($_POST['db_name']) : '';

		$result = create_database($db_host, $db_port, $db_user, $db_pass, $db_name);
		if ($result === false) {
			echo implode(',', $err->get_all());
		} else {
			echo 'OK';
		}
		break;
	case 'install_base_data' :
		$data_path = ROOT_PATH . '/install/data/weblog-php.sql';
		$sql_files = array($data_path);
		$result = install_data($sql_files);
		if ($result === false) {
			echo implode(',', $err->get_all());
		} else {
			echo 'OK';
		}
		break;
	case 'create_admin_password' :
		$admin_name         = isset($_POST['admin_name'])       ? json_str_iconv(trim($_POST['admin_name'])) : '';
		$admin_password     = isset($_POST['admin_password'])   ? trim($_POST['admin_password']) : '';
		$admin_password2    = isset($_POST['admin_password2'])  ? trim($_POST['admin_password2']) : '';
		$admin_email        = isset($_POST['admin_email'])      ? trim($_POST['admin_email']) : '';

		$result = create_admin_passport($admin_name, $admin_password, $admin_password2, $admin_email);
		if ($result === false) {
			echo implode(',', $err->get_all());
		} else {
			echo 'OK';
		}
		break;
	case 'error' :
		$err_msg = implode(',', $err->get_all());
		$smarty->assign('err_msg', $err_msg);
		$smarty->display('error.tpl');
		break;
	default :
		die('Error, unknown step!');
}
